/**
 * 从中间向两头寻找，特殊踩坑点为aaaa的情况
*/
int get_length(char* str, int pos, int length, int* start)
{
    /* 1：bab 2：baab 3：错误 */
    int type = 0;
    int left, right;
    /* 判断0位置的情况 */
    if (pos == 0) {
        *start = 0;
        if (length > 1) {
            if (str[0] == str[1]) {
                return 2;
            }
        }
        return 1;
    }
    /* 判断末尾的情况 */
    if (pos == length - 1) {
        if (length > 1) {
            if (str[pos] == str[pos - 1]) {
                *start = pos - 1;
                return 2;
            }
        }
        *start = pos;
        return 1;
    }

    int len1 = 0, len2 = 0;
    int start1 = 0, start2 = 0;
    /* 判断aba的情况 */
    if (str[pos - 1] == str[pos + 1]) {
        int counter = 1;
        while (pos - counter >= 0 && pos + counter < length) {
            if (str[pos - counter] != str[pos + counter]) {
                start1 = pos - counter + 1;
                len1 = counter * 2 - 1;
                goto next1;
            }
            counter++;
        }
        start1 = pos - counter + 1;
        len1 = counter * 2 - 1;
    }
next1:
    /* 判断aa的情况 */
    if (str[pos] == str[pos + 1]) {
        left = pos;
        right = pos + 1;
        while (left >= 0 && right < length) {
            if (str[left] != str[right]) {
                start2 = left + 1;
                len2 = right - left - 1;
                goto comp;
            }
            left--;
            right++;
        }
        start2 = left + 1;
        len2 = right - left - 1;
    }
    /* 上述情况都不符合，则为1 */
    if(len1 == 0 && len2 == 0) {
        *start = pos;
        return 1;
    }

comp:
    /* 取大的，有可能存在aaaa的情况，第一个if得到的长度为3，第二个if得到的长度为4 */
    if (len1 > len2) {
        *start = start1;
        return len1;
    }
    else {
        *start = start2;
        return len2;
    }
}

char* longestPalindrome(char* s) {
    int max = -1, length = strlen(s), temp, pos = 0, start = 0, start_ans = 0;
    int i;
    for (i = 0; i < length; i++) {
        temp = get_length(s, i, length, &start);
        if (temp > max) {
            start_ans = start;
            max = temp;
            pos = i;
        }
    }
    s[start_ans + max] = '\0';
    return s + start_ans;
}
